package servlet.events;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Debug;

public abstract class EventHandler {
	//resource bundle is used for reading the "URL.properties" file
	protected ResourceBundle urlBundle = ResourceBundle.getBundle("URL");
	protected ResourceBundle errorsBundle = ResourceBundle.getBundle("Errors");
	
	//the method "execute()" must be overriden in each BaseEventHandler's subclass
	public abstract void execute(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException;
	
	//forward back to client
	protected void forward(String url, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
				
		RequestDispatcher rd  = request.getRequestDispatcher(url);
		Debug.log(this, "forward()", "forward to " + url);
		rd.forward(request, response);
	}
	
	protected void setErrors(String errorName, HttpServletRequest request) {
		request.setAttribute(errorName, errorsBundle.getString(errorName));
	}
}
